package com.janko.springbootbase.juc.thread_basic;

import com.fasterxml.jackson.core.JsonParser;

import javax.annotation.security.RunAs;
import java.util.concurrent.*;

/**
 * @Author janko
 * @Version 1.0
 * @date 2021/5/19 23:34
 * @Description 如何创建线程
 */
public class CreateThread {

    static class MyThread extends  Thread{
        @Override
        public void run() {
            System.out.println("执行线程 ExtentThread方法");
        }
    }
    //这种更灵活一些
    static class MyRun implements Runnable{
        @Override
        public void run() {
            System.out.println("实现Runnable方法");
        }
    }

    static class MyCall implements Callable<String>{
        @Override
        public String call()  {
            System.out.println("实现了Callable的call方法");
            return "success";
        }
    }

    public static void main(String[] args) throws Exception {
//        Feture<String> f

        //1st
        new MyThread().start();
        //2sec
        new Thread(new MyRun()).start();
        new Thread(()->{
            System.out.println("Lambda 实现Thread 方法");
        }).start();
        new MyCall().call();


        //线程池创建
        Thread t=new Thread(new FutureTask<String>(new MyCall()));
        t.start();

        FutureTask<String> task=new FutureTask<>(new MyCall());
        Thread tt=new Thread(task);
        tt.start();
        System.out.println(task.get());


        //线程池2   异步执行
        ExecutorService service = Executors.newCachedThreadPool();
        service.execute(()->{
            System.out.println("Hello ThreadPool");
        });
//        service.submit(new MyCall());
        Future<String> feture=service.submit(new MyCall());
        String result= feture.get();
        System.out.println("MyCall的执行结果是："+result);
        if(result.equals("success")){
            service.shutdown();
        }

    }
}

